package com.cskaoyan.controller.back;

import com.cskaoyan.bean.bo.RoleDeleteBO;
import com.cskaoyan.bean.po.BasePageInfo;
import com.cskaoyan.bean.po.CommonData;
import com.cskaoyan.bean.po.MarketRole;
import com.cskaoyan.bean.vo.back.AdminRolePermissionsVo;
import com.cskaoyan.bean.vo.back.BaseRespVo;
import com.cskaoyan.bean.vo.back.RoleCreateVO;
import com.cskaoyan.bean.vo.back.RoleOptionsVO;
import com.cskaoyan.service.back.PermissionService;
import com.cskaoyan.service.back.RoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 12-江涛
 * @since 2022/10/12  19:21
 */
@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;

    /**
     * 显示管理员的时候显示对应的角色  与AdminController中List方法一同起作用
     * jzc
     *
     * @return {@link BaseRespVo}
     */
    @GetMapping("options")
    public BaseRespVo options() {

        CommonData<RoleOptionsVO> commonDataVO = roleService.roleOptions();
        return BaseRespVo.ok(commonDataVO);
    }

    /**
     * 角色列表
     * jzc
     *
     * @param basePageInfo 基本信息页
     * @param name         名字
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:list")
    @GetMapping("list")
    public BaseRespVo roleList(BasePageInfo basePageInfo, String name) {
        CommonData commonData = roleService.getRoleList(basePageInfo, name);
        return BaseRespVo.ok(commonData);
    }

    /**
     * 创建角色
     * jzc
     *
     * @param name 名字
     * @param desc desc
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:create")
    @PostMapping("create")
    public BaseRespVo roleCreate(@RequestBody String name, String desc) {
        RoleCreateVO roleCreateVO = roleService.CreateRole(name, desc);
        return BaseRespVo.ok(roleCreateVO);
    }

    /**
     * 显示角色的权限
     * jzc
     *
     * @param roleId 角色id
     * @return {@link BaseRespVo}<{@link AdminRolePermissionsVo}>
     */
    @RequiresPermissions("admin:role:permission:get")
    @GetMapping("permissions")
    public BaseRespVo<AdminRolePermissionsVo> rolePermissions(Integer roleId) {
        AdminRolePermissionsVo adminRolePermissionsVo = roleService.getRolePermission(roleId);
        return BaseRespVo.ok(adminRolePermissionsVo);
    }

    /**
     * 给对应角色添加授权的操作
     *
     * @param map 添加的角色id、授权的信息
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:permission:update")
    @PostMapping("permissions")
    public BaseRespVo permission(@RequestBody Map map) {
        Integer roleId = (Integer) map.get("roleId");
        List<String> permissions = (List<String>) map.get("permissions");
        permissionService.options(roleId,permissions);
        return BaseRespVo.ok();
    }


    /**
     * 修改角色的信息
     * jzc
     *
     * @param marketRole 市场作用
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:update")
    @PostMapping("update")
    public BaseRespVo roleUpadate(@RequestBody MarketRole marketRole) {
        int effected = roleService.UpadateRole(marketRole);
        if (effected == 0) {
            BaseRespVo.invalidData();
        }
        return BaseRespVo.ok(marketRole);
    }

    /**
     * 角色删除
     * jzc
     *
     * @param roleDeleteBO 角色删除博
     * @return {@link BaseRespVo}
     */
    @RequiresPermissions("admin:role:delete")
    @PostMapping("delete")
    public BaseRespVo roleDelete(@RequestBody RoleDeleteBO roleDeleteBO) {
        int effected = roleService.DeleteRole(roleDeleteBO);
        if (effected == 0) {
            BaseRespVo.error("删除失败");
        }
        return BaseRespVo.ok(roleDeleteBO);
    }


}
